package rpc.corder;

import hassion.Hession;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.channel.ChannelHandler.Sharable;
import java.io.Serializable;

/**
 * @author Jim
 * @date 16:26 2022/5/6
 * @description
 * @email:huwmz04153277@126.com
 */
@Sharable
public class MyObjectEncoder extends MessageToByteEncoder<Serializable> {
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Serializable serializable, ByteBuf byteBuf) throws Exception {
        byte[] bytes = Hession.hessionSeriallize(serializable);
        //解决粘包，半包问题，传消息长度作为消息头
        byteBuf.writeInt(bytes.length);
        byteBuf.writeBytes(bytes);

    }
}
